package com.spring.mvc.queue;

import com.spring.mvc.util.StringUtil;

public class DispatchThread extends Thread {

	private int threadId;
	private DispatchListener _listener;
	private boolean blnFlag = false;
	private Object attachment;

	public DispatchThread(int id, String desc) {
		this.threadId = id;
		this.blnFlag = true;

		setName(desc
				+ "-"
				+ StringUtil.LeftPad(new StringBuilder(String.valueOf(id))
						.toString(), "0", 2));
	}

	@Override
	public void run() {
		while (blnFlag) {
			if (this._listener != null) {
				this._listener.process(threadId);
			} else {
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void stopThread() {
		this.blnFlag = false;
		interrupt();
	}

	public void registerListener(DispatchListener listener) {
		this._listener = listener;
	}

	public void setAttachment(Object attach) {
		this.attachment = attach;
	}

	public Object getAttachment() {
		return this.attachment;
	}
}
